import { defineStore } from 'pinia'
import { UserList } from '@/config/user'
import { getUuid } from '@/utils/common'

type loginUser = (User.UserStoreInfo | undefined)

export const useUserStore = defineStore('user',{
  state: () => ({
    user: <User.UserStoreInfo>{}
  }),
  getters: {
    getUser: (state) => state.user
  },
  actions: {
    // 设置|存储用户信息
    setUser(user: User.UserStoreInfo){
      this.user = user
    },
    // 登录
    login(user: User.UserForm): loginUser{
      let userInfo: loginUser = UserList.find(item => item.username === user.username && item.password === user.password)
      if(userInfo !== undefined) {
        userInfo.token = getUuid()
      }
      return userInfo
    },
    // 退出
    logout() {
      this.user = {
        username: null,
        password: null,
        token: null
      }
    }
  },
  persist: {
    storage: localStorage,
    key: 'user'
  }
})


